<template>
  <div class="websocket-container">
    <div v-if="!isConnected">
      <form @submit.prevent="connectWebSocket">
        <div class="form-group">
          <label for="baseUrl">BaseUrl:</label>
          <input
            id="baseUrl"
            v-model="baseUrl"
            type="text"
            placeholder="请输入 WebSocket 服务地址"
            required
          />
        </div>
        <div class="form-group">
          <label for="token">Token:</label>
          <input
            id="token"
            v-model="token"
            type="text"
            placeholder="请输入 Token"
            required
          />
        </div>
        <button type="submit">连接</button>
      </form>
      <div v-if="connectionStatus" class="status">
        {{ connectionStatus }}
      </div>
    </div>
    <div v-else class="interactive-container">
      <form @submit.prevent="sendMessage">
        <div class="form-group">
          <input
            v-model="inputMessage"
            type="text"
            placeholder="请输入消息"
            required
          />
        </div>
        <button @click="disconnectWebSocket" class="disconnect-button">断开连接</button>
        <button type="submit">发送</button>
      </form>
      <div class="message-display">
        <div v-for="(message, index) in [...messages].reverse()" :key="index" class="message">
          <span class="message-label">收到消息:</span>
          <span class="message-content">{{ message }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const baseUrl = ref('')
const token = ref('')
const connectionStatus = ref('')
const isConnected = ref(false)
const inputMessage = ref('')
const messages = ref<string[]>([])
let socket: WebSocket | null = null

const connectWebSocket = () => {
  const url = `${baseUrl.value}?token=${encodeURIComponent(token.value)}`
  socket = new WebSocket(url)

  socket.onopen = () => {
    connectionStatus.value = '连接成功！'
    isConnected.value = true
  }

  socket.onmessage = (event) => {
    messages.value.push(event.data)
  }

  socket.onerror = (error) => {
    connectionStatus.value = `连接失败: ${error}`
  }

  socket.onclose = () => {
    connectionStatus.value = '连接已关闭'
    isConnected.value = false
  }
}

const sendMessage = () => {
  if (socket && socket.readyState === WebSocket.OPEN) {
    socket.send(inputMessage.value)
    inputMessage.value = ''
  }
}

const disconnectWebSocket = () => {
  if (socket) {
    socket.close()
  }
}
</script>

<style scoped>
.websocket-container {
  padding: 2rem;
  background: white;
  display: flex;
  flex-direction: column;
  overflow: auto;
}

h1 {
  color: #2c3e50;
  margin-bottom: 1.5rem;
  text-align: center;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #2c3e50;
}

input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

input:focus {
  border-color: #007bff;
  outline: none;
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

button {
  padding: 0.75rem 1.5rem;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  transition: background-color 0.3s;
  margin-right: 0.75rem;
}

button:hover {
  background-color: #0056b3;
}

.status {
  margin-top: 1.5rem;
  padding: 1rem;
  background-color: #f8f9fa;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  text-align: center;
  color: #495057;
}

.interactive-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  margin-top: 0.5rem;
}

.message-display {
  flex: 1;
  overflow-y: auto;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  background-color: #f8f9fa;
}

.message {
  margin-bottom: 0.75rem;
  padding: 0.5rem;
  background: white;
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.message-label {
  font-weight: 600;
  margin-right: 0.5rem;
  color: #007bff;
}

.disconnect-button {
  background-color: #dc3545;
}

.disconnect-button:hover {
  background-color: #c82333;
}

.loading {
  display: inline-block;
  width: 1rem;
  height: 1rem;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 1s ease-in-out infinite;
  margin-right: 0.5rem;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}
</style>
